﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PIS.TechGrid.Data
{
    using System.Data;
    using System.Data.SqlClient;

    public static class TypeMapper
    {
        public class DbTypeMapItem
        {
            #region 属性

            public SqlDbType SqlDbType
            {
                get;
                private set;
            }

            public string SqlDbTypeString
            {
                get;
                private set;
            }

            public Type CSharpType
            {
                get;
                private set;
            }

            public string CSharpTypeString
            {
                get;
                private set;
            }

            #endregion

            #region 构造函数

            public DbTypeMapItem()
            {
            }

            public DbTypeMapItem(SqlDbType sqlDbType, string sqlDbTypeString, Type cSharpType, string cSharpTypeString)
            {
                this.SqlDbType = sqlDbType;
                this.SqlDbTypeString = SqlDbTypeString;
                this.CSharpType = cSharpType;
            }

            #endregion
        }

        static HashSet<DbTypeMapItem> DbTypeMap;

        static TypeMapper()
        {
            DbTypeMap = new HashSet<DbTypeMapItem>();
        }

        static void Initialize()
        {
            // 设置SQL类型到C#类型映射
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Bit, "bit", typeof(Boolean), "boolean"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.TinyInt, "tinyint", typeof(Byte), "byte"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.SmallInt, "smallint", typeof(Int16), "Int16"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Int, "int", typeof(Int32), "int"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.BigInt, "bigint", typeof(Int64), "Int64"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.SmallMoney, "smallmoney", typeof(Decimal), "decimal"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Money, "money", typeof(Decimal), "decimal"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Decimal, "decimal", typeof(Decimal), "decimal"));
            // DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Numeric, "numeric", typeof(Decimal), "decimal"));

            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Float, "float", typeof(Double), "double"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Real, "real", typeof(Single), "Single"));

            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.SmallDateTime, "smalldatetime", typeof(DateTime), "DateTime"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Date, "date", typeof(DateTime), "DateTime"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.DateTime, "datetime", typeof(DateTime), "DateTime"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.DateTime2, "datetime2", typeof(DateTime), "DateTime"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Timestamp, "timestamp", typeof(DateTime), "DateTime"));

            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Char, "char", typeof(String), "String"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Text, "text", typeof(String), "String"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.VarChar, "varchar", typeof(String), "String"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.NChar, "nchar", typeof(String), "String"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.NText, "ntext", typeof(String), "String"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.NVarChar, "nvarchar", typeof(String), "String"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Xml, "xml", typeof(String), "String"));

            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Binary, "binary", typeof(Byte[]), "Byte[]"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.VarBinary, "varbinary", typeof(Byte[]), "Byte[]"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Image, "image", typeof(Byte[]), "Byte[]"));

            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.UniqueIdentifier, "uniqueidentifier", typeof(Guid), "Guid"));
            DbTypeMap.Add(new DbTypeMapItem(SqlDbType.Variant, "variant", typeof(Object), "Object"));
        }
    }
}
